package training.sample.app.repository.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import training.sample.app.domain.UserAccount;
import training.sample.app.repository.UserDao;
import training.sample.app.repository.base.AbstractHibernateDao;

@Repository
@Transactional
public class UserRepository extends AbstractHibernateDao<UserAccount, Long> implements UserDao {
	
	@Override
	public UserAccount findById(Long userId) {
		Criteria cr = getSession()
				.createCriteria(UserAccount.class, "users")
				.add(Restrictions.eq("userId", userId));
		return (UserAccount) cr.uniqueResult();
	}
	
	public UserAccount findByCredentials(String login, String password) {
		Criteria cr = getSession()
				.createCriteria(UserAccount.class, "users")
				.add(Restrictions.eq("login", login))
				.add(Restrictions.eq("password", password));
		return (UserAccount) cr.uniqueResult();
	}

	@Override
	public UserAccount findByLogin(String login) {
		Criteria cr = getSession()
				.createCriteria(UserAccount.class, "users")
				.add(Restrictions.eq("login", login));
		return (UserAccount) cr.uniqueResult();
	}
}

